package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerGroupMerchantMapper;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantTask;
import com.xydtech.ecard.ecard.domain.EcardApiPersonInfo;
import com.xydtech.ecard.ecard.mapper.EcardApiMerchantTaskMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiPersonInfoMapper;
import com.xydtech.ecard.ecard.service.IEcardApiPersonInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片个人信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-08-26 下午2:42
 */
@Service
public class EcardApiPersonInfoServiceImpl implements IEcardApiPersonInfoService {

    @Autowired
    private EcardApiPersonInfoMapper ecardPersonInfoMapper;
    @Autowired
    private EcardApiMerchantTaskMapper ecardMerchantTaskMapper;
    @Autowired
    private EcardApiCustomerGroupMerchantMapper ecardCustomerGroupMerchantMapper;

    /**
     * 唯一值验证，相同个人客户类型类型、支行、个人客户姓名不能重复
     * @param ecardPersonInfo
     * @return
     */
    @Override
    public int validatePersonInfo(EcardApiPersonInfo ecardPersonInfo) {
        return ecardPersonInfoMapper.validatePersonInfo(ecardPersonInfo);
    }

    /**
     * 查询电子名片个人信息
     * @param id
     * @return
     */
    @Override
    public EcardApiPersonInfo selectEcardPersonInfoById(Long id, String salt) {
        EcardApiPersonInfo ecardPersonInfo = ecardPersonInfoMapper.selectEcardPersonInfoById(id);
        try {
            //数据解密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getPerCustPhone())) {
                ecardPersonInfo.setPerCustPhone(AESEncryptionUtils.decrypt(ecardPersonInfo.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getPerCustIdcard())) {
                ecardPersonInfo.setPerCustIdcard(AESEncryptionUtils.decrypt(ecardPersonInfo.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardPersonInfo;
    }

    /**
     * 保存电子名片个人信息
     * @param ecardPersonInfo
     * @return
     */
    @Override
    public int addPersonInfo(EcardApiPersonInfo ecardPersonInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getPerCustPhone())) {
                ecardPersonInfo.setPerCustPhone(AESEncryptionUtils.encrypt(ecardPersonInfo.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getPerCustIdcard())) {
                ecardPersonInfo.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardPersonInfo.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardPersonInfoMapper.addPersonInfo(ecardPersonInfo);
    }

    /**
     * 查询电子名片个人信息列表
     * @param ecardPersonInfo
     * @return
     */
    @Override
    public List<EcardApiPersonInfo> getMyPersonList(EcardApiPersonInfo ecardPersonInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getContactsPhone())) {
                ecardPersonInfo.setContactsPhone(AESEncryptionUtils.encrypt(ecardPersonInfo.getContactsPhone(), salt));
            }
            List<EcardApiPersonInfo> personList = ecardPersonInfoMapper.getMyPersonList(ecardPersonInfo);
            if (personList.size() > 0) {
            for (EcardApiPersonInfo personInfo : personList) {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(personInfo.getContactsPhone())) {
                    personInfo.setContactsPhone(AESEncryptionUtils.decrypt(personInfo.getContactsPhone(), salt));
                }
            }
            }
            return personList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询电子名片个人信息列表
     * @param ecardPersonInfo
     * @return
     */
    @Override
    public List<EcardApiPersonInfo> getPersonList(EcardApiPersonInfo ecardPersonInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardPersonInfo.getContactsPhone())) {
                ecardPersonInfo.setContactsPhone(AESEncryptionUtils.encrypt(ecardPersonInfo.getContactsPhone(), salt));
            }
            List<EcardApiPersonInfo> personList = ecardPersonInfoMapper.getPersonList(ecardPersonInfo);
            if (personList.size() > 0) {
                for (EcardApiPersonInfo personInfo : personList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(personInfo.getContactsPhone())) {
                        personInfo.setContactsPhone(AESEncryptionUtils.decrypt(personInfo.getContactsPhone(), salt));
                    }
                }
            }
            return personList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询个人地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @param perType
     * @return
     */
    @Override
    public List<EcardApiPersonInfo> getPersonCoordinateList(Long creatorId, Long deptId, Integer perType) {
        return ecardPersonInfoMapper.getPersonCoordinateList(creatorId, deptId, perType);
    }

    /**
     * 根据法人机构ID查询机构下所有个人坐标信息列表
     * @param deptId
     * @param perType
     * @return
     */
    @Override
    public List<EcardApiPersonInfo> getAllPersonCoordinateList(Long creatorId, Long deptId, Integer perType) {
        return ecardPersonInfoMapper.getAllPersonCoordinateList(creatorId, deptId, perType);
    }

    /**
     * 根据ID修改个人状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changePersonStatus(Long id) {
        //查询是否存在未处理营销任务
        EcardApiMerchantTask merchantTask = ecardMerchantTaskMapper.getMerchantTaskByMerchantId(id, "1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        if (merchantTask!=null) {
            //存在则更新为已完成
            merchantTask.setTaskSts("1");
            ecardMerchantTaskMapper.updateEcardMerchantTask(merchantTask);
        }
        //同步修改行业客户中的状态
        EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
        groupMerchant.setMerchantId(id);
        groupMerchant.setMerchantType("1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantList(groupMerchant);
        if (customerGroupMerchantList.size() > 0) {
            ecardCustomerGroupMerchantMapper.changeIndustrialCustomerStatus(customerGroupMerchantList.get(0).getId());
        }
        return ecardPersonInfoMapper.changePersonStatus(id);
    }
}
